﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using GeneratorModels.Models;
using GeneratorModels.Properties;
using GeneratorModels.Utility;
using wintrue;

namespace GeneratorModels.Forms
{
    internal partial class Form_DBConn : WT_UI
    {
        private static DataBaseDAL DataBaseDALProp;

        internal Form_DBConn()
        {
            InitializeComponent();
            Icon = Resources.logo;
            tableLayoutPanel_DBInfo.GetType()
                .GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic)
                .SetValue(tableLayoutPanel_DBInfo, true, null);
        }

        private void Form_DBConn_Shown(object sender, EventArgs e)
        {
            textBox_DBAddress.Text = "127.0.0.1";
            textBox_DBPort.Text = "3306";
            textBox_DBName.Text = "ha666db";
            textBox_DBAccount.Text = "root";
            textBox_DBPassword.Text = "1234567890";
            textBox_ConnPath.Text = "Ha666DB";
            textBox_PackageName.Text = "models";
        }

        private void button_Conn_Click(object sender, EventArgs e)
        {
            try
            {
                #region 验证输入信息

                #region 验证数据库类型

                int dbtype = 0;
                if (rb_mysql.Checked)
                    dbtype = 1;
                if (rb_sqlserver.Checked)
                    dbtype = 2;
                if (dbtype <= 0)
                {
                    MessageBox.Show("请选择数据库类型！");
                    return;
                }
                if (dbtype == 2)
                {
                    MessageBox.Show("暂时不支持SQLServer！");
                    return;
                }

                #endregion

                #region 验证服务器

                if (string.IsNullOrWhiteSpace(textBox_DBAccount.Text))
                {
                    MessageBox.Show("请输入服务器地址！");
                    return;
                }

                #endregion

                #region 验证端口

                if (string.IsNullOrWhiteSpace(textBox_DBPort.Text))
                {
                    MessageBox.Show("请输入端口号！");
                    return;
                }

                #endregion

                #region 验证数据库

                if (string.IsNullOrWhiteSpace(textBox_DBName.Text))
                {
                    MessageBox.Show("请输入数据库名称！");
                    return;
                }

                #endregion

                #region 验证用户名

                if (string.IsNullOrWhiteSpace(textBox_DBAccount.Text))
                {
                    MessageBox.Show("请输入用户名！");
                    return;
                }

                #endregion

                #region 验证密码

                if (string.IsNullOrWhiteSpace(textBox_DBPassword.Text))
                {
                    MessageBox.Show("请输入密码！");
                    return;
                }

                #endregion

                #region 验证连接路径

                if (string.IsNullOrWhiteSpace(textBox_ConnPath.Text))
                {
                    MessageBox.Show("请输入数据库连接路径！");
                    return;
                }
                if (textBox_ConnPath.Text.Contains("."))
                {
                    if (!Regex.IsMatch(textBox_ConnPath.Text.Replace(".", "").Replace("/", ""), @"(^[A-Za-z0-9_]+$)"))
                    {
                        MessageBox.Show("数据库连接路径不正确，格式：ha666/initial.Ha666DBConn！");
                        return;
                    }
                    int start1 = textBox_ConnPath.Text.IndexOf(".");
                    if (start1 <= 0)
                    {
                        MessageBox.Show("数据库连接路径不正确，格式：ha666/initial.Ha666DBConn！");
                        return;
                    }
                    Form_Main.ConnNamespace = textBox_ConnPath.Text.Substring(0, start1);
                    int start2 = textBox_ConnPath.Text.IndexOf("/");
                    if (start2 < 0)
                    {
                        Form_Main.ConnDBName = textBox_ConnPath.Text;
                    }
                    else
                    {
                        Form_Main.ConnDBName = textBox_ConnPath.Text.Substring(start2 + 1);
                    }
                }
                else
                {
                    if (!Regex.IsMatch(textBox_ConnPath.Text, @"(^[A-Za-z0-9_]+$)"))
                    {
                        MessageBox.Show("数据库连接路径不正确，格式：Ha666DBConn！");
                        return;
                    }
                    Form_Main.ConnDBName = textBox_ConnPath.Text;
                }
                if (string.IsNullOrWhiteSpace(Form_Main.ConnDBName))
                {
                    MessageBox.Show("数据库连接路径不正确，数据库连接对象不能为空！");
                    return;
                }

                #endregion

                #region 验证包名

                if (string.IsNullOrWhiteSpace(textBox_PackageName.Text))
                {
                    MessageBox.Show("请输入包名！");
                    return;
                }
                if (!Regex.IsMatch(textBox_PackageName.Text, @"(^[A-Za-z0-9_]+$)"))
                {
                    MessageBox.Show("包名不正确！");
                    return;
                }
                Form_Main.PackageName = textBox_PackageName.Text;

                #endregion

                #endregion

                #region 生成连接字符串

                string ConnectionString = "";
                if (dbtype == 1)
                {
                    ConnectionString = string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};",
                        textBox_DBAddress.Text, textBox_DBPort.Text, textBox_DBName.Text, textBox_DBAccount.Text,
                        textBox_DBPassword.Text);
                    DataBaseDALProp = new MysqlDataBaseDAL();
                }

                #endregion

                #region 验证连接

                List<TableInfo> tableInfos = DataBaseDALProp.GetAllTableName(ConnectionString, textBox_DBName.Text);
                if (tableInfos == null || tableInfos.Count <= 0)
                {
                    MessageBox.Show("没有找到任何表！");
                    return;
                }

                #endregion

                Form_Main.ConnectionString = ConnectionString;
                Form_Main.DataBaseString = textBox_DBName.Text;
                Form_Main.DataBaseType = dbtype;
                DialogResult = DialogResult.Yes;
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("【button_Conn_Click】ex：{0}", ex);
                MessageBox.Show("异常：" + ex.Message);
            }
        }

        private void button_Quit_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}